//ya
function handler(request) {
        		    document.getElementById("textJson").value=request.responseText;
        		
                    }
//*http://openlayers.org/dev/examples/wfs-snap-split.html*/

//ya
function processRequest()
{
	var request = OpenLayers.Request.POST({
	    url: "GeoinformaticsService",
	    data: document.getElementById("output").value,
	    callback: handler
	});
	handler(request) ;
}

//frame de mapa 1	 
var map, grid, vectors, gridPoints,selectedFeature,select,popup;

//frame de mapa 2
var map2, grid2, vectors2, gridPoints2,selectedFeature2,select2,popup2;

  
        function init()//ya
        {
        	drawMap1();   
        	drawMap2();   
        }

        function drawMap1()
        {
        	map = new OpenLayers.Map('map');
            
            gridPoints = new OpenLayers.Layer.Vector("Grid result",{
            	isBaseLayer: true,
                name: "Grid points",
                styleMap: new OpenLayers.StyleMap({
                    pointRadius: 1,
                    strokeColor: "#3333ff",
                    strokeWidth: 1,
                    fillOpacity: 1,
                    fillColor: "#ffffff",
                    graphicName: "square"
                })
            });
            
            vectors = new OpenLayers.Layer.Vector("Vector Layer");
            
            map.addLayers([vectors,gridPoints]);
            
            map.addControl(new OpenLayers.Control.MousePosition());
            
            map.addControl(new OpenLayers.Control.EditingToolbar(vectors));
            
            map.addControl(new OpenLayers.Control.LayerSwitcher());
           
            
            var snap = new OpenLayers.Control.Snapping({
                layer: vectors,
                targets: [{
                    layer: gridPoints,
                    tolerance: 15
                }]
            });
            

            
            snap.activate();
            
           var options = {
        		clickout:true,
                hover: true,
                toggle:true
                ,clickFeature:serialize,
                onSelect: serialize 
            };
            
            select = new OpenLayers.Control.SelectFeature(vectors, options);
            
            map.addControl(select);
            
            select.activate();

            map.setCenter(new OpenLayers.LonLat(0, 0), 1);
        }
        
        function drawMap2()
        {
        	map2 = new OpenLayers.Map('map2');            
            gridPoints2 = new OpenLayers.Layer.Vector("Grid result",{
            	isBaseLayer: true,
                name: "Grid points",
                styleMap: new OpenLayers.StyleMap({
                    pointRadius: 1,
                    strokeColor: "#3333ff",
                    strokeWidth: 1,
                    fillOpacity: 1,
                    fillColor: "#ffffff",
                    graphicName: "square"
                })
            });
            
            vectors2 = new OpenLayers.Layer.Vector("Vector Layer");
            
            map2.addLayers([vectors,gridPoints]);
            
            map2.addControl(new OpenLayers.Control.MousePosition());
            
            map2.addControl(new OpenLayers.Control.EditingToolbar(vectors));
            
            map2.addControl(new OpenLayers.Control.LayerSwitcher());           
            
            var snap = new OpenLayers.Control.Snapping({
                layer: vectors,
                targets: [{
                    layer: gridPoints,
                    tolerance: 15
                }]
            });
            snap.activate();
            
           var options = {
        		clickout:true,
                hover: true,
                toggle:true
                ,clickFeature:serialize,
                onSelect: serialize 
            };            
            select2 = new OpenLayers.Control.SelectFeature(vectors2, options);            
            map2.addControl(select2);            
            select2.activate();
            map2.setCenter(new OpenLayers.LonLat(0, 0), 1);
        }
        
        function deleteFeature()//ya
        {   
       	     selectedFeature.popup.destroy();
       	     selectedFeature.destroy();
        }
        
        function serialize(feature) {//ya
        	selectedFeature=feature;
           // second argument for pretty printing (geojson only)
            var pretty = document.getElementById("prettyPrint").checked;
            var geojson= new OpenLayers.Format.GeoJSON();
            var str = geojson.write(feature, pretty);
            // not a good idea in general, just for this demo
            str = str.replace(/,/g, ', ');
           // this.popup = new OpenLayers.Popup.FramedCloud("featurePopup",
           // 		selectedFeature.geometry.getBounds().getCenterLonLat(),
           //         new OpenLayers.Size(100,100),
           //         "<input type=\"button\" value=\"add to DataBase\" style=\"border: #08088A 1px solid; font-family: Verdana; font-size: 9px; background-color: #DEDEDE; font-weight: bold \" onclick=\"processRequest();\" /><br><input type=\"button\" value=\"delete from memory\" style=\"border: #08088A 1px solid; font-family: Verdana; font-size: 9px; background-color: #DEDEDE; font-weight: bold \" onclick=\"deleteFeature();\" />",
           //         null, true, null);
           // selectedFeature.popup = this.popup;
           // map.addPopup(this.popup);         
            document.getElementById('output').value = str;
        }
        
        function onFeatureUnselect(feature) {//ya
            map.removePopup(feature.popup);
            feature.popup.destroy();
            feature.popup = null;
        }    
        
        function onPopupClose(evt) {//ya
            select.unselect(selectedFeature);
        }
        

        function deserializeJsonMap() {//ya
            var element = document.getElementById('textJson');
            var geojson= new OpenLayers.Format.GeoJSON();
            var features = geojson.read(element.value);          
            var s =element.value;
            var valido =0; 
            valido=s.indexOf("ResultDomainGrid");
            var bounds;
            if(features) {
                if(features.constructor != Array) {
                    features = [features];
                }
                for(var i=0; i<features.length; ++i) {
                    if (!bounds) {
                        bounds = features[i].geometry.getBounds();
                    } else {
                        bounds.extend(features[i].geometry.getBounds());
                    }

                }
                if(valido==-1)
                {
                    vectors.addFeatures(features);
                }else
                {
                  gridPoints.addFeatures(features);	
                }
                map.zoomToExtent(bounds);
                var plural = (features.length > 1) ? 's' : '';
                element.value = features.length + ' feature' + plural + ' added';
            } else {
                element.value = 'Bad input geoJson';
            }
        }

        // preload images
        (function() {
            var roots = ["draw_point", "draw_line", "draw_polygon", "pan"];
            var onImages = [];
            var offImages = [];
            for(var i=0; i<roots.length; ++i) {
                onImages[i] = new Image();
                onImages[i].src = "OpenLayers-2.11/theme/default/img/" + roots[i] + "_on.png";
                offImages[i] = new Image();
                offImages[i].src = "OpenLayers-2.11/theme/default/img/" + roots[i] + "_on.png";
            }
        })();